package com.owl.app;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageButton;
import android.widget.ImageView;

import com.owl.app.cms.util;

public class AppSite extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.main);
        setContentView(R.layout.intro);
        
        /*ImageView image = new ImageView(this);
        image.setImageResource(R.drawable.app_mark);
        setContentView(image);*/
        /*
        ImageView image = (ImageView)findViewById(R.id.img_app_mark);
        Animation alphaAnim = AnimationUtils.loadAnimation(this, R.anim.alpha);
        image.startAnimation(alphaAnim);*/
        
        ImageView image = (ImageView)findViewById(R.id.img_app_mark); 
        Animation tween = AnimationUtils.loadAnimation(this, R.anim.tween);
        image.startAnimation(tween);
        
        /*
        ImageView img = (ImageView)findViewById(R.id.anim_bg);
        img.setBackgroundResource(R.drawable.anim_list);
        
        AnimationDrawable frameAnimation = (AnimationDrawable)img.getBackground();
        frameAnimation.start();
        */
        
        ImageView img = (ImageView)findViewById(R.id.anim_bg);
        img.setBackgroundResource(R.drawable.anim_list);
        
        slideStart slideS = new slideStart();
        slideEnd slideE = new slideEnd();
        
        Timer timerS = new Timer(false);
        timerS.schedule(slideS, 1000);
        Timer timerE = new Timer(false);
        timerE.schedule(slideE, 20000);
        
        ImageButton icon1 = (ImageButton)findViewById(R.id.img_app_mark);
        icon1.setOnClickListener(new ImageButton.OnClickListener(){
        		public void onClick(View v){
        			//Intent intent = new Intent(AppSite.this, MainSite.class);
        			//startActivity(intent);
        			// progressDialog start
        			showDialog(PROGRESS_DIALOG);
        		}
        });
        
        ImageButton icon2 = (ImageButton)findViewById(R.id.anim_bg);
        icon2.setOnClickListener(new ImageButton.OnClickListener(){
        		public void onClick(View v){
        			Intent intent = new Intent(AppSite.this, MainSite.class);
        			startActivity(intent);
        		}
        });
        
        registerForContextMenu(icon2);
        
        
    }
    
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo){
    	super.onCreateContextMenu(menu, v, menuInfo);
    	MenuInflater inflater = getMenuInflater();
    	inflater.inflate(R.menu.context_menu, menu);
    }
    
    @Override
    public boolean onContextItemSelected(MenuItem item){
    	util cmsutil = new util();
    	
    	switch(item.getItemId()){
    	case R.id.goMain:
    		cmsutil.goActivity(this, this.getPackageName() + ".MainSite");
    		return true;
    	case R.id.goCompanyMap:
    		cmsutil.goActivity(this, this.getPackageName() + ".OwlMap");
    		//((owllab)this.getApplication()).setCompanyMapSate(this.getResources().getString(R.string.company_address));
    		return true;
    	case R.id.goGoogleMap:
    		cmsutil.goActivity(this, this.getPackageName() + ".OwlMap");
    		//((owllab)this.getApplication()).setCompanyMapState("");
    		return true;
    	case R.id.goOwlVideo:
    		cmsutil.goActivity(this, this.getPackageName() + ".SiteVideo");
    		return true;
    	default:
    		return super.onContextItemSelected(item);
    	}
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu){
    	
    	MenuInflater inflater = getMenuInflater();
    	inflater.inflate(R.menu.option_menu, menu);
    	
    	return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item){
    	
    	boolean tmp = new OptionMenu(this).initSelected(item);
    	if(tmp) return tmp;
    	else return super.onOptionsItemSelected(item);
    }
    
    class slideStart extends TimerTask{
    		slideStart(){
    			
    		}
    		public void run(){
    			ImageView img = (ImageView)findViewById(R.id.anim_bg);
    			AnimationDrawable frameAnimation = (AnimationDrawable)img.getBackground();
    			frameAnimation.start();
    		}
    }
    class slideEnd extends TimerTask{
	    	slideEnd(){}
	    	public void run(){
	    		ImageView img = (ImageView)findViewById(R.id.anim_bg);
    			AnimationDrawable frameAnimation = (AnimationDrawable)img.getBackground();
    			frameAnimation.stop();
	    	}
    }
    
    //ProgressDialog
    static final int PROGRESS_DIALOG = 0;
    ProgressDialog progressDialog;
    ProgressThread progressThread;
    
    private class ProgressThread extends Thread{
    	Handler mHandler;
    	final static int STATE_DONE = 0;
    	final static int STATE_RUNNING = 1;
		private static final String TAG = "ProgressThread";
    	int mState;
    	int total;
    	
    	ProgressThread(Handler h){
    		mHandler = h;
    	}
    	
    	public void run(){
    		mState = STATE_RUNNING;
    		total = 0;
    		while(mState == STATE_RUNNING){
    			try{
    				Thread.sleep(100);
    			}catch(InterruptedException e){
    				Log.e("ERROR", "Thread interrupted");
    			}
    			Message msg = mHandler.obtainMessage();
    			Bundle b =new Bundle();
    			b.putInt("total", total);
    			msg.setData(b);
    			mHandler.sendMessage(msg);
    			//Log.i(TAG, "--------------- : " + total);
    			total++;
    		}
    	}
    	
        public void setState(int state){
        	mState = state;
        }
    }
    
    protected Dialog onCreateDialog(int id){
    	switch(id){
    	case PROGRESS_DIALOG:
    		progressDialog = new ProgressDialog(this);
    		progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    		progressDialog.setMessage("Loading...");
    		progressThread = new ProgressThread(handler);
    		progressThread.start();
    		progressDialog.setMax(10);
    		return progressDialog;
    	default:
    		return null;
    	}
    }
    
    final Handler handler = new Handler(){
    	
    	@Override
    	public void handleMessage(Message msg){
    		//Log.i("HandlerMessage", "---------------$$$$$$$$$$$ : ");
    		int total = msg.getData().getInt("total");
    		progressDialog.setProgress(total);
    		if(total >= 10){
    			Intent intent = new Intent(AppSite.this, MainSite.class);
    			startActivity(intent);
    			dismissDialog(PROGRESS_DIALOG);
    			progressThread.setState(ProgressThread.STATE_DONE);
    		}
    	}
    };
}